package com.internetCafes.spms.common.AIRecognize.handler.impl;

import com.internetCafes.spms.common.CertificateTypeConstants;
import com.internetCafes.spms.common.CommonConstants;
import com.internetCafes.spms.common.utils.IdCardUtils;
import com.internetCafes.spms.common.AIRecognize.handler.URLHandler;
import com.internetCafes.spms.common.AIRecognize.recognizer.impl.Pic2WordAIRecognizer;
import com.internetCafes.spms.web.certificate.vo.AIRecognizeInfo;
import com.sms.common.util.hutool.http.HttpUtil;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author LinYe
 * @date 2020/10/14
 */
@Component
public class HtmlURLHandler implements URLHandler {

    @Autowired
    Pic2WordAIRecognizer pic2WordAIRecognizer;

    /**
     * 根据url判断对应网页，进行解析处理
     * @param url
     * @param filePath 图片路径
     * @return 智能识别实体类
     */
    @Override
    public AIRecognizeInfo handle(String url, String filePath) {
        if (url.contains(CommonConstants.QRCodeRecognizeUrlType.TRANSPORT_THREE_WEB)) {
            return handleTransportThreeWeb(url, filePath);
        }
        if (url.contains(CommonConstants.QRCodeRecognizeUrlType.NATION_CERTIFICATE_WEB)) {
            return handleNationCertificateWeb(url);
        }
        return null;
    }

    /**
     * 对交通运输三类网进行html解析，获取智能识别实体类
     * @param url 
     * @param filePath 图片路径
     * @return 智能识别实体类
     */
    private AIRecognizeInfo handleTransportThreeWeb(String url, String filePath) {
        String html = HttpUtil.get(url);
        int flag = html.indexOf("ZhengShuBianHao");
        String certificateNo = html.substring(flag + 17, flag + 31);
        flag = html.indexOf("XingMing");
        int flag2 = html.indexOf("<", flag);
        String name = html.substring(flag + 10, flag2);
        flag = html.indexOf("FaZhengShiJian");
        String releaseTime = html.substring(flag + 16, flag + 26);
        flag = html.indexOf("YouXiaoShiJian");
        String validityEndTime = html.substring(flag + 16, flag + 26);
        AIRecognizeInfo aiRecognizeInfo = new AIRecognizeInfo();
        aiRecognizeInfo.setName(name);
        aiRecognizeInfo.setCertificateNo(certificateNo);
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        aiRecognizeInfo.setpIdType(CertificateTypeConstants.PIdType.THREE_CATEGORIES_PERSONNEL);
        aiRecognizeInfo.setIdType(CertificateTypeConstants.IdType.TRANSPORT_THREE);
        if (certificateNo.contains("A")) {
            aiRecognizeInfo.setProfession(CertificateTypeConstants.Profession.COMPANY_MANAGER);
        } else if (certificateNo.contains("B")) {
            aiRecognizeInfo.setProfession(CertificateTypeConstants.Profession.PROJECT_MANAGER);
        } else {
            aiRecognizeInfo.setProfession(CertificateTypeConstants.Profession.SAFETY_PRODUCTION);
        }
        try {
            aiRecognizeInfo.setReleaseTime(format.parse(releaseTime));
            aiRecognizeInfo.setValidityEndTime(format.parse(validityEndTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        aiRecognizeInfo.setpIdType(CertificateTypeConstants.PIdTypeCode.THREE_CATEGORIES_PERSONNEL);
        aiRecognizeInfo.setIdType(CertificateTypeConstants.IdType.TRANSPORT_THREE);
        AIRecognizeInfo recognize = pic2WordAIRecognizer.recognize(filePath);
        String idCard = recognize.getIdCard();
        if (idCard!=null) {
            aiRecognizeInfo.setIdCard(idCard);
        }
        return aiRecognizeInfo;
    }

    /**
     * 对国家证书网进行html解析，获取智能识别实体类
     * 1、发起第一次请求，根据省份号获取二次请求路径
     * 2、做第二次模拟请求，获取modelId
     * 3、根据modelId发起三次请求获取最终结果
     * @param url
     * @return 智能识别实体类
     */
    private AIRecognizeInfo handleNationCertificateWeb(String url) {
        String html = HttpUtil.get(url.replace("&amp;", "&"));
        String provinceNO = url.substring(29, 31);
        String urlByProvince = getURLByProvince(provinceNO);
        String id = url.substring(39, 45);
        int i = html.indexOf("id_m");
        String id_m = html.substring(i + 25, i + 43);
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", id);
        paramMap.put("id_m", id_m);
        html = HttpUtil.post(urlByProvince, paramMap);

        int modelIdIndex = html.indexOf("modelId");
        String modelId = html.substring(modelIdIndex + 16, modelIdIndex + 40);

        html=HttpUtil.get("http://zj.sowin.com:80/code!detail.do?id_s="+modelId);
        if (html.contains("页面出错了")){
            return null;
        }
        int nameIndex = html.indexOf("姓名");
        int start = html.substring(nameIndex).indexOf("<td>");
        int end = html.substring(start+nameIndex).indexOf("</td>");
        String name = html.substring(start+nameIndex+4, start+end+nameIndex);
        int certificateNoIndex = html.indexOf("证书编号");
        String certificateNo = html.substring(certificateNoIndex + 20, certificateNoIndex + 36);
        int idCardIndex = html.indexOf("身份证号");
        String idCard = html.substring(idCardIndex + 20, idCardIndex + 38);
        int professionIndex = html.indexOf("职业");
        start = html.substring(professionIndex).indexOf("<td>");
        end = html.substring(start+professionIndex).indexOf("</td>");
        String profession = html.substring(start+professionIndex+4, start+end+professionIndex).trim();
        int releaseTimeIndex = html.indexOf("颁证日期");
        String releaseTime=html.substring(releaseTimeIndex+21,releaseTimeIndex+31);

        AIRecognizeInfo aiRecognizeInfo=new AIRecognizeInfo();
        aiRecognizeInfo.setName(name);
        aiRecognizeInfo.setCertificateNo(certificateNo);
        if (IdCardUtils.validateCard(idCard)){
            aiRecognizeInfo.setIdCard(idCard);
        }
        aiRecognizeInfo.setProfession(profession);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
        Date releaseDate = null;
        try {
            releaseDate = simpleDateFormat.parse(releaseTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        aiRecognizeInfo.setpIdType(CertificateTypeConstants.PIdTypeCode.TECHNICIAN);
        aiRecognizeInfo.setIdType(CertificateTypeConstants.IdType.TECHNICIAN_NATION);
        aiRecognizeInfo.setReleaseTime(releaseDate);
        return aiRecognizeInfo;
    }

    private String getURLByProvince(String province) {
        String url = "";
        if (province.equals("6")) {//a=6四川
            url = "http://sc.sowin.com/code!cer.do";
        } else if (province.equals("2")) {//a=2山东
            url = "http://sd.sowin.com/code!cer.do";
        } else if (province.equals("3")) {//a=3天津
            url = "http://tj.sowin.com/code!cer.do";
        } else if (province.equals("5") || province.equals("1")) {//a=5或1 湖北
            url = "http://hb.sowin.com/code!cer.do";
        } else if (province.equals("5000")) {//a=5000湖北
            url = "http://hb.sowin.com/code!cer.do";
        } else if (province.equals("7")) {//a=7新疆
            url = "http://xj.sowin.com/code!cer.do";
        } else if (province.equals("8")) {//a=8广东
            url = "http://gd.sowin.com/code!cer.do";
        } else if (province.equals("9")) {//a=9江西
            url = "http://jx.sowin.com/code!cer.do";
        } else if (province.equals("10")) {//a=10黑龙江省
            url = "http://hlj.sowin.com/code!cer.do";
        } else if (province.equals("11")) {//a=11河北省
            url = "http://heb.sowin.com/code!cer.do";
        } else if (province.equals("12")) {//a=12福建省
            url = "http://fj.sowin.com/code!cer.do";
        } else if (province.equals("13")) {//a=13上海
            url = "http://sh.sowin.com/code!cer.do";
        } else if (province.equals("13000")) {//a=13000上海
            url = "http://sh.sowin.com/code!cer.do";
        } else if (province.equals("15")) {//a=15卫生部
            url = "http://ws.sowin.com/code!cer.do";
        } else if (province.equals("16")) {//a=16军队
            url = "http://ws.sowin.com/code!cer.do";
        } else if (province.equals("17")) {//a=17民航
            url = "http://mh.sowin.com/code!cer.do";
        } else if (province.equals("18")) {//a=18北京
            url = "http://219.232.200.51:8081/bjzxdyxt/code!cer.do";
        } else if (province.equals("19")) {//a=19有色
            url = "http://ys.sowin.com/code!cer.do";
        } else if (province.equals("20")) {//a=20化工
            url = "http://hg.sowin.com/code!cer.do";
        } else if (province.equals("21")) {//a=21云南
            url = "http://yn.sowin.com/code!cer.do";
        } else if (province.equals("22")) {//a=22农业
            url = "http://ny.sowin.com/code!cer.do";
        } else if (province.equals("23")) {//a=23内蒙古
            url = "http://nmg.sowin.com/code!cer.do";
        } else if (province.equals("25")) {//a=25辽宁
            url = "http://ln.sowin.com/code!cer.do";
        } else if (province.equals("26")) {//a=26林业
            url = "http://ly.sowin.com/code!cer.do";
        } else if (province.equals("27")) {//a=27冶金
            url = "http://yj.sowin.com/code!cer.do";
        } else if (province.equals("28")) {//a=28纺织
            url = "http://fz.sowin.com/code!cer.do";
        } else if (province.equals("29")) {//a=29中商联
            url = "http://zsl.sowin.com/code!cer.do";
        } else if (province.equals("30")) {//a=30轻工
            url = "http://qg.sowin.com/code!cer.do";
        } else if (province.equals("31")) {//a=31河南
            url = "http://hn.sowin.com/code!cer.do";
        } else if (province.equals("32")) {//a=32新闻
            url = "http://xw.sowin.com/code!cer.do";
        } else if (province.equals("33")) {//a=33文化
            url = "http://wh.sowin.com/code!cer.do";
        } else if (province.equals("35")) {//a=35青海
            url = "http://qh.sowin.com/code!cer.do";
        } else if (province.equals("36")) {//a=36宁夏
            url = "http://nx.sowin.com/code!cer.do";
        } else if (province.equals("37")) {//a=37海南
            url = "http://hns.sowin.com/code!cer.do";
        } else if (province.equals("38")) {//a=38机械
            url = "http://jix.sowin.com/code!cer.do";
        } else if (province.equals("39")) {//a=39广西
            url = "http://gx.sowin.com/code!cer.do";
        } else if (province.equals("50")) {//a=50陕西
            url = "http://sx.sowin.com/code!cer.do";
        } else if (province.equals("51")) {//a=51江苏
            url = "http://js.sowin.com/code!cer.do";
        } else if (province.equals("52")) {//a=52广电
            url = "http://gad.sowin.com/code!cer.do";
        } else if (province.equals("53")) {//a=53消防
            url = "http://xf.sowin.com/code!cer.do";
        } else if (province.equals("55")) {//a=55总装
            url = "http://zsdy.cavtc.cn/code!cer.do";
        } else if (province.equals("56")) {//a=56安徽
            url = "http://ah.sowin.com/code!cer.do";
        } else if (province.equals("57")) {//a=57国家测绘局
            url = "http://ch.sowin.com/code!cer.do";
        } else if (province.equals("58")) {//a=58山西省
            url = "http://sxs.sowin.com/code!cer.do";
        } else if (province.equals("59")) {//a=59通信
            url = "http://tx.sowin.com/code!cer.do";
        } else if (province.equals("60")) {//a=60船舶重工
            url = "http://cz.sowin.com/code!cer.do";
        } else if (province.equals("61")) {//a=61电子
            url = "http://dz.sowin.com/code!cer.do";
        } else if (province.equals("62")) {//a=62测试系统
            url = "http://test.sowin.com/code!cer.do";
        } else if (province.equals("63")) {//a=63贵州
            url = "http://gz.sowin.com/code!cer.do";
        } else if (province.equals("65")) {//a=65重庆
            url = "http://cq.sowin.com/code!cer.do";
        } else if (province.equals("66")) {//a=66粮食局
            url = "http://ls.sowin.com/code!cer.do";
        } else if (province.equals("67")) {//a=67甘肃
            url = "http://gs.sowin.com/code!cer.do";
        } else if (province.equals("68")) {//a=68浙江
            url = "http://zj.sowin.com/code!cer.do";
        } else if (province.equals("1888")) {//a=1888浙江
            url = "http://zj.sowin.com/code!cer.do";
        } else if (province.equals("69")) {//a=69国家邮政管理局
            url = "http://yz.sowin.com/code!cer.do";
        } else if (province.equals("70")) {//a=70紧急救援
            url = "http://jy.sowin.com/code!cer.do";
        } else if (province.equals("71")) {//a=71交通
            url = "http://jt.sowin.com/code!cer.do";
        } else if (province.equals("72")) {//a=72湖南
            url = "http://hun.sowin.com/code!cer.do";
        } else if (province.equals("73")) {//a=73兵团
            url = "http://bt.sowin.com/code!cer.do";
        } else if (province.equals("75")) {//a=75吉林
            url = "http://jl.sowin.com/code!cer.do";
        } else if (province.equals("76")) {//a=76国土资源
            url = "http://gt.sowin.com/code!cer.do";
        } else if (province.equals("77")) {//a=77建材
            url = "http://jc.sowin.com/code!cer.do";
        } else if (province.equals("78")) {//a=78珠宝
            url = "http://zb.sowin.com/code!cer.do";
        } else if (province.equals("79")) {//a=79兵器工业
            url = "http://bq.sowin.com/code!cer.do";
        } else if (province.equals("80")) {//a=80中直机关
            url = "http://zjg.sowin.com/code!cer.do";
        } else if (province.equals("81")) {//a=81卫计委
            url = "http://wjw.sowin.com/code!cer.do";
        } else if (province.equals("82")) {//a=82烟草
            url = "http://yc.sowin.com/code!cer.do";
        } else if (province.equals("83")) {//a=83邮政集团
            url = "http://yzjt.sowin.com/code!cer.do";
        } else if (province.equals("85")) {//a=85水利部
            url = "http://slzyzg.mwr.gov.cn/code!cer.do";
        } else if (province.equals("86")) {//a=86航天科工
            url = "http://hk.sowin.com/code!cer.do";
        } else if (province.equals("87")) {//a=87供销社
            url = "http://gxs.sowin.com/code!cer.do";
        } else if (province.equals("88")) {//a=88体育总局
            url = "http://www.sportosta-zscx.cn/code!cer.do";
        } else if (province.equals("89")) {//a=89中铁建
            url = "http://ztj.sowin.com/code!cer.do";
        } else if (province.equals("90")) {//a=90电力
            url = "http://dl.sowin.com/code!cer.do";
        } else if (province.equals("91")) {//a=91西藏
            url = "http://xzz.sowin.com/code!cer.do";
        } else if (province.equals("92")) {//a=92中国中铁
            url = "http://zt.sowin.com/code!cer.do";
        } else if (province.equals("93")) {//a=93中国兵器装备集团
            url = "http://bqzb.sowin.com/code!cer.do";
        } else if (province.equals("95")) {//a=95质量
            url = "http://zl.sowin.com/code!cer.do";
        } else if (province.equals("96")) {//a=96中建
            url = "http://zjz.sowin.com/code!cer.do";
        } else if (province.equals("97")) {//a=97中交集团
            url = "http://zjs.sowin.com/code!cer.do";
        } else if (province.equals("98")) {//a=98中航工业
            url = "http://zh.sowin.com/code!cer.do";
        } else if (province.equals("99")) {//a=99船舶工业
            url = "http://cg.sowin.com/code!cer.do";
        } else if (province.equals("100")) {//a=100民政
            url = "http://mzz.sowin.com/code!cer.do";
        } else if (province.equals("101")) {//a=101上海飞机
            url = "http://sf.sowin.com/code!cer.do";
        } else if (province.equals("102")) {//a=102部中心指导处
            url = "http://zdc.sowin.com/code!cer.do";
        } else if (province.equals("103")) {//a=103中国铁路总公司
            url = "http://tl.sowin.com/code!cer.do";
        } else if (province.equals("105")) {//a=105国管局
            url = "http://ggj.sowin.com/code!cer.do";
        } else if (province.equals("106")) {//a=106中医药局
            url = "http://zy.sowin.com/code!cer.do";
        } else if (province.equals("107")) {//a=107中国冶金科工股份有限公司
            url = "http://zyz.sowin.com/code!cer.do";
        } else if (province.equals("108")) {//a=108神华集团
            url = "http://shjt.sowin.com/code!cer.do";
        } else if (province.equals("109")) {//a=109煤炭
            url = "http://mt.sowin.com/code!cer.do";
        } else if (province.equals("110")) {//a=110中海油
            url = "http://zj.sowin.com/code!cer.do";
        } else if (province.equals("1100")) {//a=1100中海油
            url = "http://zhy.sowin.com/code!cer.do";
        } else if (province.equals("111")) {//a=111乐凯集团
            url = "http://lk.sowin.com/code!cer.do";
        } else if (province.equals("112")) {//a=112军队平安里
            url = "http://lk.sowin.com/code!cer.do";
        } else if (province.equals("113")) {//a=113核工业
            url = "http://lk.sowin.com/code!cer.do";
        } else if (province.equals("115")) {//a=115军队干部在职教育职业技能鉴定中心
            url = "http://qj.sowin.com/code!cer.do";
        } else if (province.equals("116")) {//a=116中国铁路通信信号集团公司省/行业职业技能鉴定（指导）中心
            url = "http://ztx.sowin.com/code!cer.do";
        } else if (province.equals("117")) {//a=117公安
            url = "http://ztx.sowin.com/code!cer.do";
        } else if (province.equals("118")) {//a=118中国联合网络通信集团有限公司
            url = "http://ltz.sowin.com/code!cer.do";
        } else if (province.equals("119")) {   //a=119中国航天科技集团公司
            url = "http://ltz.sowin.com/code!cer.do";
        } else if (province.equals("120")) {   //a=120中车
            url = "http://zc.sowin.com:8181/code!cer.do";
        } else if (province.equals("121")) {   //a=121武警
            url = "http://wj.sowin.com/code!cer.do";
        } else if (province.equals("122")) {   //a=122中远海运公司职业技能鉴定（指导）中心
            url = "http://zyy.sowin.com/code!cer.do";
        } else if (province.equals("123")) {   //a=123中国航天科技集团公司
            url = "http://zyy.sowin.com/code!cer.do";
        } else if (province.equals("125")) {   //a=125消防部队
            url = "http://xfj.sowin.com/code!cer.do";
        } else if (province.equals("126")) {   //a=126烟草等级
            url = "http://ycdj.sowin.com/code!cer.do";
        } else if (province.equals("127")) {   //a=127中海油技能等级
            url = "http://hy.sowin.com/code!cer.do";
        } else if (province.equals("201")) {   //a=201北京证书监管系统测试
            url = "http://cerbj.sowin.com/query!code.do";
        } else if (province.equals("203")) {   //a=203四川省能源投资集团有限则热公司
            url = "http://ct.sowin.com/code!cer.do";
        } else if (province.equals("301")) {   //a=301建材考务系统打印
            url = "http://jck.sowin.com/query!code.do";
        } else if (province.equals("501")) {   //a=501国网四川
            url = "http://gwsc.sowin.com/code!cer.do";
        }
        return url;
    }
}
